<template>
    <div v-loading.fullscreen.lock="fullscreenLoading" element-loading-text="正在发布...">
        <el-button type="success"  plain @click="updateClick()" :disabled="show">批量发布</el-button>
    </div>
</template>

<script>
import { ref } from '@vue/reactivity'
import { computed, warn, watch } from '@vue/runtime-core'
import { useStore } from 'vuex'
import { ElMessageBox,ElMessage, ElNotification } from 'element-plus'
import axios from 'axios';
export default {
    setup(){
        const store = useStore()
        let show = ref(true)
        watch(()=>store.state.questionBank.subjectModel.subjectTable.selectList,(newVal)=>{
            if(newVal.length == 0){
                show.value = true
                return
            }
            let arr = newVal.filter((val)=>{
                return val.status == 1
            })
            if(arr.length > 0){
                 show.value = true
            }else{
                show.value = false
            }
            //show.value = false
        })
        function updateClick(){
            ElMessageBox.confirm(
                '确认发布?',
                '确认',
                {
                    confirmButtonText: '确认',
                    cancelButtonText: '取消',
                    type: 'warning',
                }
            ).then(() => {
                let subjectIdLists = []
                store.state.questionBank.subjectModel.subjectTable.selectList.forEach((val)=>{
                    subjectIdLists.push(val.subjectId)
                })
                fullscreenLoading.value = true
                axios.get("/api/subject/statusListSubject", {
                    params: {
                        subjectIdList: subjectIdLists.toString(),
                        subjectTypeId: store.state.questionBank.subjectModel.subjectPagination.subjectTypeId,
                        accountTypeId: store.state.questionBank.subjectModel.subjectPagination.accountTypeId,
                        status: store.state.questionBank.subjectModel.subjectPagination.status,
                        pageNumber: store.state.questionBank.subjectModel.subjectPagination.pageNum,
                        pageSize: store.state.questionBank.subjectModel.subjectPagination.pageSize
                    }
                }).then((response) => {
                    if (response.status == 200) {
                        if (response.data.code == 200) {
                            store.commit("selectSubjectTableData", response.data.data.list);
                            store.commit("selectSubjectTableTotal", response.data.data.total);
                            ElNotification({
                                title: '操作成功',
                                message: '发布成功',
                                type: 'success',
                            })
                        } else {
                            ElNotification({
                                title: '操作失败',
                                message: '发布失败',
                                type: 'error',
                            })
                        }
                    } else {
                        ElNotification({
                            title: '操作异常',
                            message: '发布异常',
                            type: 'warring',
                        })
                    }
                }).finally(() => {
                    fullscreenLoading.value = false
                })

            }).catch(() => {
                ElMessage('操作取消')
                fullscreenLoading.value = false
            })
        }
        let fullscreenLoading = ref(false)
        return {
            show,
            fullscreenLoading,
            updateClick
        }
    }
}
</script>

<style>

</style>